package han.sql;

import han.entity.*;

import java.sql.*;
import java.util.*;

import static com.han.http.util.Print.printlnErr;
import static com.han.http.util.Print.printlnInfo;

/**
 * @fileName: Operator
 * @author: Han
 * @date: 2018/6/26 9:51
 * @dscription: 表操控类
 */
public class Operator {

    private static final Operator OPERATOR = new Operator();

    private Operator() {
    }

    public static Operator getInstance() {
        return OPERATOR;
    }

    public static void main(String... args) {
        Operator operator = Operator.getInstance();
        FinanceProjectLabelEntity entity = new FinanceProjectLabelEntity();
        entity.setId(2333);
        entity.setLabel("电爱爱爱爱啊啊商");
        entity.setTotalPages(-1);
        entity.setCurrentPage(1);
        entity.setLastRequestTime(new Timestamp(System.currentTimeMillis()));
        operator.insertFinanceProjectLabel(entity);
        DataSource.closePool();
        operator.insertFinanceProjectLabel(entity);
    }

    public List<FinanceProjectIdEntity> queryAllFinanceProjectId() {
        //获取数据库连接
        Connection connection = DataSource.getConnection();

        Statement statement;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            DataSource.close(connection);
            e.printStackTrace();
            return null;
        }
        ResultSet resultSet;
        try {
            resultSet = statement.executeQuery("SELECT * FROM m_finance_project_id " +
                    "where delete_flag = 0 order by project_id asc");
        } catch (SQLException e) {
            try {
                statement.close();
            } catch (SQLException ee) {
                ee.printStackTrace();
            }
            DataSource.close(connection);
            e.printStackTrace();
            return null;
        }
        List<FinanceProjectIdEntity> list = new LinkedList<>();
        try {
            while (resultSet.next()) {
                //解析值
                int id = resultSet.getInt("id");
                String krId = resultSet.getString("project_id");
                int times = resultSet.getInt("times");
                Timestamp lastTime = resultSet.getTimestamp("last_request_time");
                Timestamp addTime = resultSet.getTimestamp("add_time");
                int deleteFlag = resultSet.getInt("delete_flag");
                String city = resultSet.getString("city");
                int label = resultSet.getInt("label");
                //生成对象
                FinanceProjectIdEntity entity = new FinanceProjectIdEntity();
                entity.setId(id);
                entity.setProjectId(krId);
                entity.setTimes(times);
                entity.setAddTime(addTime);
                entity.setLastRequestTime(lastTime);
                entity.setDeleteFlag(deleteFlag);
                entity.setCity(city);
                entity.setLabelId(label);
                //存储
                list.add(entity);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DataSource.close(connection);
        }
        return list;
    }

    public List<FinanceProjectInfoEntity> queryProjectInfoByCondition(int c) {

        //获取数据库连接
        Connection connection = DataSource.getConnection();

        Statement statement;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            DataSource.close(connection);
            e.printStackTrace();
            return null;
        }

        ResultSet resultSet;
        String sql;
        if (c == 0) {
            sql = "select * from m_finance_project_info";
        } else if (c == 1){
            sql = "select * from m_finance_project_info where email = '' and phone_number = '' and address = ''";
        }else if (c == 2){
            sql = "select * from m_finance_project_info where email != '' and phone_number != '' and address != ''";
        }else {
            printlnErr("查询参数错误");
            return null;
        }
        try {
            resultSet = statement.executeQuery(sql);
        } catch (SQLException e) {
            try {
                statement.close();
            } catch (SQLException ee) {
                ee.printStackTrace();
            }
            DataSource.close(connection);
            e.printStackTrace();
            return null;
        }
        List<FinanceProjectInfoEntity> infoEntityList = new ArrayList<>();
        try {
            while (resultSet.next()) {
                String projectId = resultSet.getString("project_id");
                String name = resultSet.getString("name");
                String fullName = resultSet.getString("full_name");
                String website = resultSet.getString("website");
                String industryTag = resultSet.getString("industryTag");
                String email = resultSet.getString("email");
                String phoneNumber = resultSet.getString("phone_number");
                String address = resultSet.getString("address");
                String area1 = resultSet.getString("area1");
                String area2 = resultSet.getString("area2");
                String intro = resultSet.getString("intro");
                String userMarket = resultSet.getString("user_market");
                String productService = resultSet.getString("product_service");
                String financeExperience = resultSet.getString("finance_experience");
                String news = resultSet.getString("news");
                String member = resultSet.getString("member");
                Timestamp updateTime = resultSet.getTimestamp("update_time");
                int m = resultSet.getInt("m");
                FinanceProjectInfoEntity entity = new FinanceProjectInfoEntity(projectId, name, fullName, website,
                        industryTag, email, phoneNumber, address, area1, area2, intro, userMarket, productService,
                        financeExperience, news, member, updateTime);
                entity.setM(m);
                infoEntityList.add(entity);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DataSource.close(connection);
        }

        return infoEntityList;
    }

    public void insertFinanceProjectId(Collection<FinanceProjectIdEntity> collection) {
        //获取数据库连接
        Connection connection = DataSource.getConnection();

        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement("replace into m_finance_project_id (project_id,times," +
                    "delete_flag,last_request_time,add_time,city,label) values (?,?,?,?,?,?,?);");
            Iterator<FinanceProjectIdEntity> iterator = collection.iterator();
            int i = 0;
            while (iterator.hasNext()) {
                FinanceProjectIdEntity idEntity = iterator.next();
                //设置属性
                statement.setString(1, idEntity.getProjectId());
                statement.setInt(2, idEntity.getTimes());
                statement.setInt(3, idEntity.getDeleteFlag());
                statement.setTimestamp(4, idEntity.getLastRequestTime());
                statement.setTimestamp(5, idEntity.getAddTime());
                statement.setString(6, idEntity.getCity());
                statement.setInt(7, idEntity.getLabelId());
                statement.addBatch();
                i++;
                if (i % 2000 == 0) {
                    //每2000执行一次
                    statement.executeBatch();
                }
            }
            //执行剩余的
            statement.executeBatch();
            //提交
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                Objects.requireNonNull(statement).close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DataSource.close(connection);
        }
    }

    public void updateFinanceProjectId(Collection<FinanceProjectIdEntity> collection) {
        //获取数据库连接
        Connection connection = DataSource.getConnection();

        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement("update m_finance_project_id set project_id = ?,times = ?" +
                    ",delete_flag = ?,last_request_time = ?,add_time = ?,city = ? where id = ?");
            int i = 0;
            for (FinanceProjectIdEntity idEntity : collection) {
                //设置属性
                statement.setString(1, idEntity.getProjectId());
                statement.setInt(2, idEntity.getTimes());
                statement.setInt(3, idEntity.getDeleteFlag());
                statement.setTimestamp(4, idEntity.getLastRequestTime());
                statement.setTimestamp(5, idEntity.getAddTime());
                statement.setString(6, idEntity.getCity());
                statement.setInt(7, idEntity.getLabelId());
                statement.addBatch();
                i++;
                if (i % 2000 == 0) {
                    //每2000执行一次
                    statement.executeBatch();
                }
            }
            //执行剩余的
            statement.executeBatch();
            //提交
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                Objects.requireNonNull(statement).close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DataSource.close(connection);
        }
    }

    public void insertFinanceProjectInfo(Collection<FinanceProjectInfoEntity> collection) {
        //获取数据库连接
        Connection connection = DataSource.getConnection();

        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement("replace into m_finance_project_info " +
                    "(project_id, name, full_name, website, industryTag, email, phone_number, address, " +
                    "area1, area2, intro, user_market, product_service, finance_experience, news, member, " +
                    "update_time,m) VALUE (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            int count = 0;
            for (FinanceProjectInfoEntity entity : collection) {
                preparedStatement.setString(1, entity.getProjectId().replace("'", "\\'"));
                preparedStatement.setString(2, entity.getName().replace("'", "\\'"));
                preparedStatement.setString(3, entity.getFullName().replace("'", "\\'"));
                preparedStatement.setString(4, entity.getWebsite().replace("'", "\\'"));
                preparedStatement.setString(5, entity.getIndustryTag().replace("'", "\\'"));
                preparedStatement.setString(6, entity.getEmail().replace("'", "\\'"));
                preparedStatement.setString(7, entity.getPhoneNumber().replace("'", "\\'"));
                preparedStatement.setString(8, entity.getAddress().replace("'", "\\'"));
                preparedStatement.setString(9, entity.getArea1().replace("'", "\\'"));
                preparedStatement.setString(10, entity.getArea2().replace("'", "\\'"));
                preparedStatement.setString(11, entity.getIntro().replace("'", "\\'"));
                preparedStatement.setString(12, entity.getUserMarket().replace("'", "\\'"));
                preparedStatement.setString(13, entity.getProductService().replace("'", "\\'"));
                preparedStatement.setString(14, entity.getFinanceExperience().replace("'", "\\'"));
                preparedStatement.setString(15, entity.getNews().replace("'", "\\'"));
                preparedStatement.setString(16, entity.getMember().replace("'", "\\'"));
                preparedStatement.setTimestamp(17, entity.getUpdateTime());
                preparedStatement.setInt(18, entity.getM());
                preparedStatement.addBatch();
                if (count++ > 2000) {
                    //大于2000时,执行
                    preparedStatement.executeBatch();
                }
            }
            //执行最后不足2000的
            preparedStatement.executeBatch();
            //提交
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                Objects.requireNonNull(preparedStatement).close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DataSource.close(connection);
        }
    }

    public void updateFinanceProjectInfo(FinanceProjectInfoEntity entity) {

        //获取数据库连接
        Connection connection = DataSource.getConnection();

        Statement statement;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            DataSource.close(connection);
            return;
        }
        String id = entity.getProjectId();
        String email = entity.getEmail();
        String phone = entity.getPhoneNumber();
        String address = entity.getAddress();
        String sql = "update m_finance_project_info set email = '" + email + "',address='" + address +
                "',phone_number='" + phone + "' where project_id='" + id + "';";
        try {
            statement.executeUpdate(sql);
        } catch (SQLException e) {
            printlnErr(sql);
            e.printStackTrace();
        } finally {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public List<InvestCompanyIdEntity> queryAllInvestCompanyId() {
        //获取数据库连接
        Connection connection = DataSource.getConnection();

        Statement statement;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        ResultSet resultSet;
        try {
            resultSet = statement.executeQuery("SELECT * FROM m_invest_company_id where delete_flag = 0");
        } catch (SQLException e) {
            try {
                statement.close();
            } catch (SQLException ee) {
                ee.printStackTrace();
            }
            DataSource.close(connection);
            e.printStackTrace();
            return null;
        }
        List<InvestCompanyIdEntity> list = new LinkedList<>();
        try {
            while (resultSet.next()) {
                //解析值
                int id = resultSet.getInt("id");
                String krId = resultSet.getString("company_id");
                int times = resultSet.getInt("times");
                Timestamp lastTime = resultSet.getTimestamp("last_request_time");
                int deleteFlag = resultSet.getInt("delete_flag");
                //生成对象
                InvestCompanyIdEntity entity = new InvestCompanyIdEntity(id, krId, times, lastTime, deleteFlag);
                //存储
                list.add(entity);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DataSource.close(connection);
        }
        return list;
    }

    public List<InvestCompanyInfoEntity> queryAllCompanyInfo() {
        //获取数据库连接
        Connection connection = DataSource.getConnection();

        Statement statement;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            DataSource.close(connection);
            return null;
        }
        ResultSet resultSet;
        try {
            resultSet = statement.executeQuery("select * from m_invest_company_info");
        } catch (SQLException e) {
            try {
                statement.close();
            } catch (SQLException ee) {
                ee.printStackTrace();
            }
            DataSource.close(connection);
            e.printStackTrace();
            return null;
        }
        List<InvestCompanyInfoEntity> companyInfoEntityList = new ArrayList<>();
        try {
            while (resultSet.next()) {
                long id = resultSet.getLong("id");
                String name = resultSet.getString("name");
                String krId = resultSet.getString("36kr_id");
                String fullName = resultSet.getString("full_name");
                String intro = resultSet.getString("intro");
                String address = resultSet.getString("address");
                String email = resultSet.getString("email");
                String phoneNumber = resultSet.getString("phone_number");
                String member = resultSet.getString("member");
                String website = resultSet.getString("website");
                String industry = resultSet.getString("industry");
                InvestCompanyInfoEntity entity = new InvestCompanyInfoEntity(id, krId, name, fullName, intro, address,
                        email, phoneNumber, member);
                entity.setIndustry(industry);
                entity.setWebsite(website);
                companyInfoEntityList.add(entity);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DataSource.close(connection);
        }

        return companyInfoEntityList;
    }

    public List<InvestCompanyInfoEntity> queryAllTempCompanyInfo() {
        //获取数据库连接
        Connection connection = DataSource.getConnection();

        Statement statement;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            DataSource.close(connection);
            return null;
        }
        ResultSet resultSet;
        try {
            resultSet = statement.executeQuery("select * from m_invest_company_info_copy1");
        } catch (SQLException e) {
            try {
                statement.close();
            } catch (SQLException ee) {
                ee.printStackTrace();
            }
            DataSource.close(connection);
            e.printStackTrace();
            return null;
        }
        List<InvestCompanyInfoEntity> companyInfoEntityList = new ArrayList<>();
        try {
            while (resultSet.next()) {
                long id = resultSet.getLong("id");
                String name = resultSet.getString("name");
                String krId = resultSet.getString("36kr_id");
                String fullName = resultSet.getString("full_name");
                String intro = resultSet.getString("intro");
                String address = resultSet.getString("address");
                String email = resultSet.getString("email");
                String phoneNumber = resultSet.getString("phone_number");
                String member = resultSet.getString("member");
                String website = resultSet.getString("website");
                String industry = resultSet.getString("industry");
                InvestCompanyInfoEntity entity = new InvestCompanyInfoEntity(id, krId, name, fullName, intro, address,
                        email, phoneNumber, member);
                entity.setIndustry(industry);
                entity.setWebsite(website);
                companyInfoEntityList.add(entity);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DataSource.close(connection);
        }

        return companyInfoEntityList;
    }

    public void updateCompanyInfo(InvestCompanyInfoEntity entity) {
        Connection connection = null;
        Statement statement = null;
        long id = entity.getId();
        String email = entity.getEmail();
        String phoneNumber = entity.getPhoneNumber();
        String address = entity.getAddress();
        String sql = "update m_invest_company_info set email = '" + email + "',phone_number = '" + phoneNumber +
                "', address = '" + address + "' where" + " id =" + id;
        try {
            connection = DataSource.getConnection();
            statement = connection.createStatement();
            statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                Objects.requireNonNull(statement).close();
            } catch (SQLException ignore) {
            }
        }
        try {
            Objects.requireNonNull(connection).close();
        } catch (SQLException ignore) {
        }
    }

    public void insertInvestCompanyId(Collection<InvestCompanyIdEntity> collection) {
        //获取数据库连接
        Connection connection = DataSource.getConnection();

        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement("insert into m_invest_company_id (company_id,times," +
                    "last_request_time,delete_flag) values (?,?,?,?);");
            Iterator<InvestCompanyIdEntity> iterator = collection.iterator();
            int i = 0;
            while (iterator.hasNext()) {
                InvestCompanyIdEntity idEntity = iterator.next();
                //设置属性
                statement.setString(1, idEntity.getCompanyId());
                statement.setInt(2, idEntity.getTimes());
                statement.setTimestamp(3, idEntity.getLastRequestTime());
                statement.setInt(4, idEntity.getDeleteFlag());
                statement.addBatch();
                i++;
                if (i % 2000 == 0) {
                    //每2000执行一次
                    statement.executeBatch();
                }
            }
            //执行剩余的
            statement.executeBatch();
            //提交
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                Objects.requireNonNull(statement).close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DataSource.close(connection);
        }
    }

    public void updateInvestCompanyId(Collection<InvestCompanyIdEntity> collection) {
        //获取数据库连接
        Connection connection = DataSource.getConnection();

        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement("update m_invest_company_id" +
                    " set company_id = ?,times = ?,last_request_time = ?,delete_flag = ? where id = ?");
            int count = 0;
            for (InvestCompanyIdEntity entity : collection) {
                preparedStatement.setString(1, entity.getCompanyId());
                preparedStatement.setInt(2, entity.getTimes());
                preparedStatement.setTimestamp(3, entity.getLastRequestTime());
                preparedStatement.setInt(4, entity.getDeleteFlag());
                preparedStatement.addBatch();
                if (count++ > 2000) {
                    preparedStatement.executeBatch();
                }
            }
            preparedStatement.executeBatch();
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                Objects.requireNonNull(preparedStatement).close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DataSource.close(connection);
        }

    }

    public void insertInvestCompanyInfo(Collection<InvestCompanyInfoEntity> collection) {
        //获取数据库连接
        Connection connection = DataSource.getConnection();

        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement("replace into m_invest_company_info (36kr_id, name, " +
                    "full_name, intro, address, email, phone_number, member,industry,website)" +
                    " values (?,?,?,?,?,?,?,?,?,?);");
            Iterator<InvestCompanyInfoEntity> iterator = collection.iterator();
            int i = 0;
            while (iterator.hasNext()) {
                InvestCompanyInfoEntity idEntity = iterator.next();
                //设置属性
                statement.setString(1, idEntity.getKrId());
                statement.setString(2, idEntity.getName());
                statement.setString(3, idEntity.getFullName());
                statement.setString(4, idEntity.getIntro());
                statement.setString(5, idEntity.getAddress());
                statement.setString(6, idEntity.getEmail());
                statement.setString(7, idEntity.getPhoneNumber());
                statement.setString(8, idEntity.getMember());
                statement.setString(9, idEntity.getIndustry());
                statement.setString(10, idEntity.getWebsite());
                statement.addBatch();
                i++;
                if (i % 2000 == 0) {
                    //每2000执行一次
                    statement.executeBatch();
                }
            }
            //执行剩余的
            statement.executeBatch();
            //提交
            connection.commit();
        } catch (Exception e) {
            printlnInfo(statement);
            e.printStackTrace();
        } finally {
            try {
                Objects.requireNonNull(statement).close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DataSource.close(connection);
        }
    }

    /**
     * 插入融资项目标签
     *
     * @param entity 融资项目标签实体
     */
    public void insertFinanceProjectLabel(FinanceProjectLabelEntity entity) {

        //获取数据库连接
        Connection connection = DataSource.getConnection();

        Statement statement;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            DataSource.close(connection);
            return;
        }
        String label = entity.getLabel();
        int totalPages = entity.getTotalPages();
        int currentPages = entity.getCurrentPage();
        Timestamp timestamp = entity.getLastRequestTime();
        //插入标签
        String sqlStr = String.format("insert into m_finance_project_label (label, total_pages, " +
                        "current_page, last_request_time)" +
                        " values ('%s',%d,%d,'%tY-%tm-%td %tH:%tM:%tS')", label, totalPages, currentPages, timestamp,
                timestamp, timestamp, timestamp, timestamp, timestamp);
        try {
            statement.executeUpdate(sqlStr);
            connection.commit();
        } catch (SQLException e) {
            printlnInfo(sqlStr);
            e.printStackTrace();
        } finally {
            try {
                Objects.requireNonNull(statement).close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DataSource.close(connection);
        }
    }

    /**
     * 更新标签表
     *
     * @param entity 标签实体
     */
    public void updateFinanceProjectLabel(FinanceProjectLabelEntity entity) {

        //获取数据库连接
        Connection connection = DataSource.getConnection();

        Statement statement;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            DataSource.close(connection);
            return;
        }
        int id = entity.getId();
        String label = entity.getLabel();
        int totalPages = entity.getTotalPages();
        int currentPages = entity.getCurrentPage();
        Timestamp timestamp = entity.getLastRequestTime();
        //插入标签
        String sqlStr = String.format("update m_finance_project_label set label = '%s',total_pages = %d," +
                        "current_page = %d,last_request_time = '%tY-%tm-%td %tH:%tM:%tS' where id = %d",
                label, totalPages, currentPages, timestamp,
                timestamp, timestamp, timestamp, timestamp, timestamp, id);
        try {
            statement.executeUpdate(sqlStr);
            connection.commit();
        } catch (SQLException e) {
            printlnInfo(sqlStr);
            e.printStackTrace();
        } finally {
            try {
                Objects.requireNonNull(statement).close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DataSource.close(connection);
        }
    }

    /**
     * 查询融资项目标签
     *
     * @return 融资项目标签
     */
    public List<FinanceProjectLabelEntity> queryFinanceProjectLabel() {

        //获取数据库连接
        Connection connection = DataSource.getConnection();

        Statement statement;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            DataSource.close(connection);
            e.printStackTrace();
            return null;
        }
        ResultSet resultSet;
        String sqlStr = "select * from m_finance_project_label where label = '医疗健康'";
        try {
            resultSet = statement.executeQuery(sqlStr);
        } catch (SQLException e) {
            try {
                statement.close();
            } catch (SQLException ee) {
                ee.printStackTrace();
            }
            DataSource.close(connection);
            e.printStackTrace();
            return null;
        }
        List<FinanceProjectLabelEntity> entityList = new ArrayList<>();
        //查询总页数为-1(没有被查询的标签)或者当前页数小于总页数
        try {
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String label = resultSet.getString("label");
                int totalPages = resultSet.getInt("total_pages");
                int currentPage = resultSet.getInt("current_page");
                Timestamp lastRequestTime = resultSet.getTimestamp("last_request_time");
                FinanceProjectLabelEntity entity = new FinanceProjectLabelEntity();
                entity.setId(id);
                entity.setLabel(label);
                entity.setTotalPages(totalPages);
                entity.setCurrentPage(currentPage);
                entity.setLastRequestTime(lastRequestTime);
                entityList.add(entity);
            }
        } catch (SQLException e) {
            printlnInfo(sqlStr);
            e.printStackTrace();
        } finally {
            try {
                Objects.requireNonNull(resultSet).close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                Objects.requireNonNull(statement).close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DataSource.close(connection);
        }

        return entityList;
    }
}
